﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using Bussiness;
using Bussiness.Service;

namespace Bussiness.View.BackEnd
{
    public partial class Product_Detail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Appcontex app = (Appcontex)Session["app"];
            if (!IsPostBack)  pu_ngayNhap.SelectedDate = DateTime.Now;
            

            if (app.IsAdmin == false)
            {
                btSave.Visible = false;
                Button1.Visible = false;
                pu_Button2.Visible = false;
                pu_Button3.Visible = false;
            }


            string action = Request.Params.Get("Action");
            string id = Request.Params.Get("ID");
            
            
            if (!X.IsAjaxRequest)
            {
                if (action != null && id != null)
                {
                    this.pu_id.Value = id;
                    if (action == "Edit") Edit_Products(int.Parse(id));
                    
                }
                //danh muc cha
                pu_LoaiSP_Store.DataSource = new Catelogy_Service().getCateloriesProductParent();
                pu_LoaiSP_Store.DataBind();

                
                
            } 
        }
        public void SelectParent(object sender, DirectEventArgs e)
        { 
            pu_DMCon_Store.DataSource = new Catelogy_Service().getCatelogiesProductChild(int.Parse(pu_LoaiSP_Grid.SelectedItem.Value));
            pu_DMCon_Store.DataBind();
        }

        public void save(object sender, DirectEventArgs e)
        {
            Product pr = new Product();
            int checkCode = 1;
            if (!Formater.isNull(pu_id.Text)) pr.ID = int.Parse(pu_id.Text);
            
            // kiem tra ma san pham da ton tai chua
            if (Formater.isNull(pu_id.Text))
            {   
                string code = "";    
                if(new Product_Service().getProductByCode(pu_maHang.Text) != null)
                code = new Product_Service().getProductByCode(pu_maHang.Text).Code;
                if ( code == pu_maHang.Text)
                {
                    checkCode = 0;
                    MessageBox ms = new MessageBox();
                    ms.Alert("Lỗi", "Mã sản phẩm này đã tồn tại");
                    ms.Show();
                }
            }

            if(checkCode != 0){
            pr.Name = pu_tenHang.Text;
            pr.Code = pu_maHang.Text;
            pr.IdCategory = int.Parse(this.pu_DMCon_Grid.SelectedItem.Value);
            pr.Count = int.Parse(pu_soLuong.Text);
            pr.Unit = pu_donViTinh.Text;
            pr.DateImport = pu_ngayNhap.SelectedDate;
            pr.PriceExprort = decimal.Parse(pu_giaBan.Text);
            pr.PriceImport = decimal.Parse(pu_giaNhap.Text);
            if (!Formater.isNull(pu_giamGia.Text))
            pr.Sale = Int32.Parse(pu_giamGia.Text);
            if (pr.Count == 0) pr.Status = false; else pr.Status = true;
            pr.Description = pu_moTaSP.Text;

            Dictionary<string, string>[] valueGrid = JSON.Deserialize<Dictionary<string, string>[]>(e.ExtraParams["valueGrid"]);
            List<SizeProduct> listSizeProduce = new List<SizeProduct>();
            
            foreach (var value in valueGrid)
            {
                SizeProduct size = new SizeProduct();
                foreach (KeyValuePair<string, string> keyValuePair in value)
                {

                    if (keyValuePair.Key.Equals("Size"))
                    {
                        size.Size = keyValuePair.Value;
                    }
                    if (keyValuePair.Key.Equals("Count"))
                    {
                        size.Count = int.Parse(keyValuePair.Value);
                    }
                }
                listSizeProduce.Add(size);
            }

            if (listSizeProduce.Count == 0) pr.HaveSize = false;
            else pr.HaveSize = true;

            int checkSave = new Product_Service().save_product(pr, listSizeProduce);
            pu_id.Text = checkSave.ToString();
            MessageBox mgb = new MessageBox();
            if (checkSave != 0)
            {
                mgb.Alert("", "Lưu thành công");
                mgb.Show();
            }
            }

        }


        public void Edit_Products(int id)
        {
            Product pr = new Product_Service().getProductByID(id);
            pu_id.Text = pr.ID.ToString();
            pu_tenHang.Text = pr.Name;
            pu_maHang.Text = pr.Code;
            pu_giamGia.Text = pr.Sale.ToString();

            string ParentId = new Catelogy_Service().getCatelogy((int)pr.IdCategory).ParentId.ToString();
            pu_LoaiSP_Grid.SelectedItem.Value = ParentId;

            pu_DMCon_Store.DataSource = new Catelogy_Service().getCatelogiesProductChild(int.Parse(ParentId));
            pu_DMCon_Store.DataBind();

            pu_DMCon_Grid.SelectedItem.Value = pr.IdCategory.ToString();
            pu_soLuong.Text = pr.Count.ToString();
            pu_donViTinh.Text = pr.Unit;
            pu_ngayNhap.SelectedDate = pr.DateImport.Value;
            pu_giaBan.Text = pr.PriceExprort.ToString();
            pu_giaNhap.Text = pr.PriceImport.ToString();
            if (pr.Count == 0) pr.Status = false; else pr.Status = true;
            pu_moTaSP.Text = pr.Description;

            pu_sizeSL_Store.DataSource = new Product_Service().getListSizeByProductID(pr.ID);
            pu_sizeSL_Store.DataBind();

           
        }


        

    }
}
